package cn.xzqwjw.taskmanager.controller.api;

import cn.xzqwjw.taskmanager.domain.pojo.SysFrontRoute;
import cn.xzqwjw.taskmanager.service.SysFrontRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author rush
 */
@RestController
@RequestMapping("/api/front-route")
@Validated
public class SysFrontRouteController {

  private final SysFrontRouteService frontRouteService;

  @Autowired
  public SysFrontRouteController(SysFrontRouteService frontRouteService) {
    this.frontRouteService = frontRouteService;
  }

  /**
   * 处理 "/api/front-route" 的 POST 请求，添加前端路由
   */
  @PostMapping("")
  public String add(
      @NotNull(message = "参数不能为空")
      @Valid @RequestBody SysFrontRoute sysFrontRoute) {
    return frontRouteService.add(sysFrontRoute);
  }

  /**
   * 处理 "/api/front-route/{id}" 的 DELETE 请求，删除前端路由
   */
  @DeleteMapping("/{id:[0-9]+}")
  public String delById(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @PathVariable Integer id) {
    return frontRouteService.delById(id);
  }

  /**
   * 处理 "/api/front-route" 的 PUT 请求，修改前端路由信息
   */
  @PutMapping("")
  public String modifyAll(
      @NotNull(message = "参数不能为空")
      @Valid @RequestBody SysFrontRoute sysFrontRoute) {
    return frontRouteService.modifyAll(sysFrontRoute);
  }

  // ==================== 查询 ====================

  /**
   * 根据id得到前端路由详情
   */
  @GetMapping("/{id:[0-9]+}")
  public String queryById(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @PathVariable Integer id) {
    return frontRouteService.queryById(id);
  }

  /**
   * 得到前端路由分页数据 url类似：/list-page?PageCurrent=2&PageSize=20
   */
  @GetMapping("/list-page")
  public String listPage(
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize) {
    return frontRouteService.listPage(pageCurrent, pageSize);
  }

  @GetMapping("/list-all")
  public String listAll() {
    return frontRouteService.listAll();
  }

}
